<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Attack_model extends CI_Model
{
    function __construct()
    {
        parent::MY_Model();
    }
	function ww_get_res_user_info($uid)
	{
		return $this->getRows("CALL ww_get_res_user_info($uid)", 0);
	}
	function ww_update_res_by_hour($uid, $food, $wood, $metal, $petroleum, $time)
	{
		$this->exeQuery("CALL ww_update_res_by_hour($uid, $food, $wood, $metal, $petroleum, $time);");
	}
	function getRows($query, $multiRow = 1, $resultType = 'array')
	{
		$iconn = $this->db->conn_id;
		mysqli_multi_query($iconn, $query) or show_custom_error(mysqli_error($iconn));
		$result = mysqli_store_result($iconn);
		$data = array();
		$function = "mysqli_fetch_".$resultType;
		if($multiRow)
			while($row = $function($result))
			{
				$data[] = $row;
			}
		else $data = $function($result);
		
		mysqli_free_result($result);
		if(mysqli_more_results($iconn))
		{
			mysqli_next_result($iconn);
		}
		return $data;
	}
	
	function getRowsOnMultiQuery($query, $resultType = 'array')
	{
		$data = array();
		$function = "mysqli_fetch_".$resultType;
		
		$iconn = $this->db->conn_id;
		mysqli_multi_query($iconn, $query) or show_custom_error(mysqli_error($iconn));
		$i = 0;
		while (($result = mysqli_store_result($iconn)) !== false)
		{
			$data[$i] = array();
			while($row = $function($result))
			{
				$data[$i][] = $row;
			}
			$i++;
			if(!mysqli_next_result($iconn)) break;
		}
		
		if($result !== false) mysqli_free_result($result);
		if(mysqli_more_results($iconn))
		{
			mysqli_next_result($iconn);
		}
		return $data;
	}
	
	function exeQuery($query)
	{
		$iconn = $this->db->conn_id;
		mysqli_multi_query($iconn, $query) or $this->show_custom_error(mysqli_error($iconn));
		
		if(mysqli_more_results($iconn))
		{
			mysqli_next_result($iconn);
		}
	}
	
	function show_custom_error($mess = '')
	{
		$CI =& get_instance();
		if (class_exists('CI_DB') AND isset($CI->db))
		{
			$CI->db->close();
		}
		if (class_exists('CI_DB') AND isset($CI->db_slave))
		{
			$CI->db_slave->close();
		}
		$showCustomError = $CI->config->item('show_custom_error');
		if($showCustomError)
		{
			die($mess);
		}
		else
		{
			show_error('');
			die();
		}
	}
}